package com.summli.basic.class06;

/**
 * 将单向链表按某值划分成左边小、中间相等、右边大的形式
 *
 * 1）把链表放入数组里，在数组上做partition（笔试用）
 *
 * 2）分成小、中、大三部分，再把各个部分之间串起来（面试用）
 */
public class Code03_SmallerEqualBigger {

    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

    //=========================plan A==================================
    /*
    链表问题，我们可以准备一个数组，可以在数组上进行荷兰国旗问题，但是使用了额外的空间，并且不能维持稳定性
     */
    public static Node listPartition1(Node head, int pivot) {
        if(head == null){
            return head;
        }
        int l =0;
        Node cur = head;
        while(cur != null){
            l++;
            cur = cur.next;
        }
        Node[] helpNodes = new Node[l];
        cur = head;
        int index = 0;
        while(cur != null){
            helpNodes[index++] = cur;
            cur = cur.next;
        }
        partition(helpNodes,pivot);
        for (index = 1; index < l; index++) {
            helpNodes[index -1].next = helpNodes[index];
        }
        helpNodes[index -1].next = null;
        return helpNodes[0];
    }

    /*
    经典的荷兰国旗问题
     */
    private static void partition(Node[] nodes, int pivot) {
        int left = -1;
        int right = nodes.length;
        int index = 0;
        while(index < right){
            if(nodes[index].value < pivot){
                swap(nodes,++left,index++);
            }else if(nodes[index].value == pivot){
                index++;
            }else{
                swap(nodes,--right,index);
            }
        }
    }

    private static void swap(Node[] nodes, int left, int right) {
        Node temp = nodes[left];
        nodes[left] = nodes[right];
        nodes[right] = temp;
    }

    //============================plan B==================================
    /*
    将小等大分为三部分，一次使用头和尾指针进行记录，可以不使用额外的空间，也可以维持稳定性
     */
    public static Node listPartition2(Node head, int pivot) {
        Node sH = null;
        Node sT = null;
        Node eH = null;
        Node eT = null;
        Node bH = null;
        Node bT = null;
        Node next = null;
        while(head != null){
            next = head.next;
            head.next = null;
            if(head.value < pivot){
                if(sH == null){
                    sH = head;
                    sT = head;
                }else{
                    sT.next = head;
                    sT = head;
                }
            }else if(head.value == pivot){
                if(eH == null){
                    eH = head;
                    eT = head;
                }else{
                    eT.next = head;
                    eT = head;
                }
            }else{
                if(bH == null){
                    bH = head;
                    bT = head;
                }else{
                    bT.next = head;
                    bT = head;
                }
            }
            head = next;
        }
        if(sT != null){
            sT.next = eH;
            eT = eT == null?sT:eT;
        }
        if(eT != null){
            eT.next = bH;
        }

        return sH ==null?(eH == null?bH:eH):sH;
    }

    // for Test
    public static void printLinkedList(Node node) {
        System.out.print("Linked List: ");
        while (node != null) {
            System.out.print(node.value + " ");
            node = node.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head1 = new Node(7);
        head1.next = new Node(9);
        head1.next.next = new Node(1);
        head1.next.next.next = new Node(8);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(2);
        head1.next.next.next.next.next.next = new Node(5);
        printLinkedList(head1);

        System.out.println("======================");
//        Node result1 = listPartition1(head1, 5);
//        printLinkedList(result1);

        Node result2 = listPartition2(head1, 5);
        printLinkedList(result2);

    }
}
